<div class="filter">
    <div class="f20 item">
        <button type="button" class="am-btn am-btn-primary">新增</button>
    </div>
</div>

<!--列表-->
<table class="am-table am-table-bordered am-table-radius am-table-striped" id="result-table">
    <thead>
        <tr>
            <th>排序</th>
            <th>商户ID</th>
            <th>充值地址</th>
            <th>当前充值总额</th>
            <th>报警额度</th>
            <th>手续费</th>
            <th>最小充值额</th>
            <th>最大充值额</th>
            <th>前端展示</th>
            <th>商户类型</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

<script>
    function _updateChargeList() {
        CMD.pay_query(function(r) {
            var table = $("#result-table tbody"); table.empty();
            var list = r.list;
            list.sort(function(a, b) {
                return (b.sort || 0) - (a.sort || 0);
            });
            for (var i in list) {
                var data = list[i];
                tr = $("<tr key='" + data.id + "'><tr>");
                tr.append("<td>" + (data.sort || 0) + "</td>");

                tr.append("<td>" + data.mchId + "</td>");
                tr.append("<td>" + data.noticeUrl + "</td>");
                tr.append("<td></td>");
                tr.append("<td>" + data.warning + "</td>");
                tr.append("<td>" + (data.fee || 0) * 100 + "%</td>");
                tr.append("<td>" + data.min + "</td>");
                tr.append("<td>" + data.max + "</td>");
                tr.append("<td>" + data.name + "</td>");

                if (data.type == 'heyifuu')
                    tr.append("<td>合易付</td>");
                else if (data.type == 'sslsf')
                    tr.append("<td>闪付</td>");
                else if (data.type == 'xinmapay')
                    tr.append("<td>新码</td>");

                tr.append("<td>" + (data.enable == '1' ? "开启" : "关闭") + "</td>");
                var oper = '<button class="edit" value="__id__">编辑</button>\
                <button class="delete" value="__id__">删除</button>\
                ';
                oper = oper.replace(/__id__/g, data.id);
                tr.append("<td>" + oper + "</td>");
                table.append(tr[0]);
            }

            $("#result-table .edit").click(function(e) {
                // 编辑
                var id = $(e.currentTarget).attr("value");
                for (var i in list) {
                    if (id != list[i].id) continue;
                    context.center.chargeOper = 'edit';
                    context.center.chargeInfo = list[i];
                    Util.addPopUp("add_charge_type", "add_charge_type", 400);
                }
            });
            $("#result-table .delete").click(function(e) {
                // 删除
                if (!confirm("确定删除本条目？")) return;
                var id = $(e.currentTarget).attr("value");
                CMD.pay_delete(id, function() {
                    _updateChargeList();
                });
            });
        });
    }
    _updateChargeList();

    $(".filter .f20 button").click(function() {
        parent._updateChargeList = _updateChargeList;
        context.center.chargeOper = 'add';
        Util.addPopUp("add_charge_type", "add_charge_type", 400);
    })
</script>
